package com.qububu.db.dao.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;

import com.qububu.db.dao.DineSummarizeDao;
import com.qububu.play.dineTogether.dto.DineSummarizeDto;
import com.qububu.utils.ParamConverter;
import com.qububu.utils.SystemUtil;

public class DineSummarizeDaoImpl implements DineSummarizeDao{

	private JdbcTemplate jdbcTemplate;
	
	public void setDataSource(DataSource dataSource) {
		this.jdbcTemplate = new JdbcTemplate(dataSource);
	}
	
	public void addSummarize(DineSummarizeDto dineSummarize) {
		long actionId = ParamConverter.convertToLongObj(SystemUtil.decrypt(dineSummarize.getActionId()));
		long authorId = ParamConverter.convertToLongObj(SystemUtil.decrypt(dineSummarize.getAuthorId()));
		String sql = "insert into action_summarize(content, pictures, create_dttm, action_id, author_id) values(?, ?, ?, ?, ?)";
		Object[] data = {
				dineSummarize.getContent(),
				dineSummarize.getPictures(),
				dineSummarize.getCreateDt(),
				actionId,
				authorId
		};
		
		jdbcTemplate.update(sql, data);
	}
	
	public DineSummarizeDto getSummarizeByActionId(String actionId) {
		long reActionId = ParamConverter.convertToLongObj(SystemUtil.decrypt(actionId));
		String sql = "select * from action_summarize where action_id=?";
		Map<String, Object> resultMap = jdbcTemplate.queryForMap(sql, reActionId);
		DineSummarizeDto dineSummarize = null;
		if (!resultMap.isEmpty() && resultMap.size() == 1) {
			long summarizeId = (Long) resultMap.get("summarize_id");
			long authorId = (Long) resultMap.get("author_id");
			dineSummarize = new DineSummarizeDto(summarizeId, reActionId, authorId);
			dineSummarize.setContent(String.valueOf(resultMap.get("content") ) );
			dineSummarize.setCreateDt(Integer.parseInt(String.valueOf(resultMap.get("create_dttm") ) ) );
			dineSummarize.setPictures(String.valueOf(resultMap.get("pictures") ) );
		}
		
		return dineSummarize;
	}
	
	public void modifySummarize(DineSummarizeDto dineSummarize) {
		long actionId = ParamConverter.convertToLongObj(SystemUtil.decrypt(dineSummarize.getActionId()));
		long authorId = ParamConverter.convertToLongObj(SystemUtil.decrypt(dineSummarize.getAuthorId()));
		String sql = "update action_summarize set content=?, pictures=?, create_dttm=?, author_id=? where action_id=?";
		Object[] data = {
				dineSummarize.getContent(),
				dineSummarize.getPictures(),
				dineSummarize.getCreateDt(),
				authorId,
				actionId
				};
		jdbcTemplate.update(sql, data);
	}
	
	public boolean deleteSummarize(String summarizeId) {
		
		String sql = "delete from action_summarize where summarize_id = ?";
		
		long reSummarizeId = ParamConverter.convertToLongObj(SystemUtil.decrypt(summarizeId));
		
		int result = jdbcTemplate.update(sql, reSummarizeId);
		
		if (result == 1) {
			return true;
		}
		
		return false;
	}

	public List<DineSummarizeDto> getDineSummarizes() {
		String sql = "SELECT * FROM action_summarize ORDER BY create_dttm DESC LIMIT 0,5";
		List<Map<String, Object>> resultList = jdbcTemplate.queryForList(sql);
		List<DineSummarizeDto> dineSummarizeList = new ArrayList<DineSummarizeDto>();
		DineSummarizeDto dineSummarize = null;
		for(Map<String, Object> resultMap : resultList) {
			long summarizeId = (Long) resultMap.get("summarize_id");
			long authorId = (Long) resultMap.get("author_id");
			long actionId = (Long) resultMap.get("action_id");
			dineSummarize =new DineSummarizeDto(summarizeId, actionId, authorId);
			dineSummarize.setContent(String.valueOf(resultMap.get("content") ) );
			dineSummarize.setCreateDt(Integer.parseInt(String.valueOf(resultMap.get("create_dttm") ) ) );
			dineSummarize.setPictures(String.valueOf(resultMap.get("pictures") ) );
			dineSummarizeList.add(dineSummarize);
		}
		
		return dineSummarizeList;
	}
}
